package dao;

import dao.imp.IJobDao;
import entity.Employee;
import entity.Job;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.util.List;

/**
 * @program: personnel_management_system
 * @description: job opt
 * @author: Mr.Sun
 * @create: 2020-05-25 14:58
 **/

public class JobDao implements IJobDao {
    private JdbcTemplate template = new JdbcTemplate(MysqlConnUtil.getDataSource());

    public JobDao() throws Exception {
    }

    @Override
    public boolean insert(Job job) {

        String sql = "insert into job(old_job,new_job,emp_id) values(?,?,?)";
        Integer count = template.update(sql,job.getOld_job(),job.getNew_job(),job.getEmp_id());
        return count == 1;
    }

    @Override
    public boolean update(Job job) {
        boolean res = false;
        String sql = "UPDATE job SET old_job = ? , new_job = ? ,emp_id =? WHERE j_id = ?";

        int count = template.update(sql,job.getOld_job(),job.getNew_job(),job.getEmp_id(),job.getJ_id());
        res = count == 1;
        return res;
    }

    @Override
    public boolean delete(Job job) {
        boolean res =false;
        String sql = "DELETE FROM job WHERE j_id = ?";
        int count = template.update(sql,job.getJ_id());
        return count == 1;
    }

    @Override
    public List<Job> selectAll() {
        String sql = "select * from job";
        List<Job> jobs = template.query(sql, new BeanPropertyRowMapper<Job>(Job.class));
        return jobs;

    }

    @Override
    public List<Job> select(Job job) {
        return null;
    }
}
